<template>
  <el-dialog
    :modal="withModal"
    :visible.sync="visible"
    custom-class="rounded-lg sq-shadow"
    :close-on-click-modal="false"
    width="360px"
    @close="close"
  >
    <div slot="title">
      <img src="@/assets/img/sq.png" alt="" />
    </div>
    <el-form
      ref="form"
      label-position="top"
      label-width="80px"
      :model="form"
      class="my-form"
      :rules="rules"
    >
      <el-form-item label="单位名称" prop="companyName">
        <el-input
          placeholder="请输入公司名称"
          v-model="form.companyName"
        ></el-input>
      </el-form-item>
      <el-form-item label="联系方式" prop="phoneNum">
        <el-input
          placeholder="请输入您的手机号"
          v-model="form.phoneNum"
        ></el-input>
      </el-form-item>
      <el-form-item label="您的称呼" prop="name">
        <el-input placeholder="请输入您的姓名" v-model="form.name"></el-input>
      </el-form-item>
      <el-button type="primary " class="mt-[30px] !w-full" @click="onSubmit">
        提交</el-button
      >
      <div class="mt-4 text-center text-xs text-gray-99">
        请完善信息，我们核实后尽快为您开通账号
      </div>
    </el-form>
  </el-dialog>
</template>

<script>
function checkPhoneNum(phone) {
  if (phone === "") {
    return false;
  }
  if (!/^1[3456789]\d{9}$/.test(phone)) {
    $notice({
      type: "warn",
      content: "请输入正确的手机号~",
    });
    return false;
  } else {
    return true;
  }
}
export default {
  data() {
    var checkPhoneNum = (rule, value, callback) => {
      if (value === "" || !/^1[3456789]\d{9}$/.test(value)) {
        callback(new Error("请输入正确的手机号"));
      }
      callback();
    };
    return {
      visible: false,
      form: {
        companyName: "",
        phoneNum: "",
        name: "",
      },
      rules: {
        companyName: [
          { required: true, message: "请输入公司名称", trigger: "blur" },
        ],
        phoneNum: [{ validator: checkPhoneNum, trigger: "blur" }],
        name: [{ required: true, message: "请输入您的姓名", trigger: "blur" }],
      },
    };
  },
  computed: {
    withModal() {
      if (this.$store.state.zx) {
        return false;
      }
      return true;
    },
  },
  methods: {
    open() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs.form.resetFields();
      });
    },
    close() {
      this.$store.commit("SQ", false);
      this.visible = false;
    },

    onSubmit() {
      this.$refs["form"].validate((valid) => {
        if (!valid) {
          return;
        }
        this.postForm();
      });
    },

    postForm() {
      fetch("https://www.baijiayun.com/default/consult/add", {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify(this.form),
      })
        .then((res) => {
          // const data = res.json();
          this.$message({
            type: "success",
            message:
              "提交成功，我们的工作人员会马上与您联系，并为您开通体验账号~",
          });
          this.close();
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
};
</script>

<style scoped>
.sq-shadow {
  box-shadow: -10px 20px 60px 0px rgba(110, 133, 168, 0.08);
}
/deep/ .el-dialog__body {
  padding: 10px 30px 28px;
}
/deep/ .el-form-item {
  margin-bottom: 15px;
}
/deep/ .el-form--label-top .el-form-item__label {
  padding: 0 0 4px 0;
}
/deep/ .el-input__inner {
  background: #f8f8f8;
}
/deep/ .el-dialog__headerbtn {
  font-size: 24px;
}
/deep/ .el-form-item__label {
  font-size: 12px;
}
</style>